﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KeMei.Common;
using KeMei.DBUtility;

public partial class Manager_flashscreen_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserInfo"] == null)
        {
            Response.Redirect(ConfigHelper.SystemUrl + "Manager/login.aspx", true);

        }
        if (!IsPostBack)
        {
            string ID = Request["ID"];
            hyplnkLook.Hidden = true;
            lnkDelete.Hidden = true;
            if (!string.IsNullOrEmpty(ID))
            {
                BindModel(int.Parse(KeMei.Common.PageValidate.InputText(ID, 10)));
                filePhoto.Required = false;
            }
        }
    }

    private void BindModel(int ID)
    {
        KeMei.BLL.FlashScreen bll = new KeMei.BLL.FlashScreen();

        KeMei.Model.FlashScreen model = null;

        try
        {
            model = bll.GetModel(ID);
            if (model != null)
            {
                txtName.Text = model.TName;
                cbIsShow.Checked = model.IsShow;
                if (!string.IsNullOrEmpty(model.PicUrl))
                {
                    hfPicUrl.Text = model.PicUrl;
                    hyplnkLook.NavigateUrl = ConfigHelper.SystemUrl + model.PicUrl;
                    hyplnkLook.Hidden = false;
                    lnkDelete.Hidden = false;

                }
            }
        }
        catch (Exception ex)
        {

            ExtAspNet.Alert.Show("绑定实体类失败：" + ex.Message);
        }
    }

    protected void SaveFile(out string filePath)
    {
        bool fileOK = false;
        filePath = string.Empty;
        if (!(filePhoto.ShortFileName.Length > 0) && Request["ID"] != null)
        {
            filePath = hfPicUrl.Text;
            return;

        }
        if (!string.IsNullOrEmpty(hfPicUrl.Text.Trim()))
        {
            FileHelper.DeleteFile(Server.MapPath(ConfigHelper.SystemUrl + hfPicUrl.Text));
        }

        string fileExtension = System.IO.Path.GetExtension(filePhoto.FileName).ToLower();
        String[] allowedExtensions = { ".jpg", ".jpeg", ".png", ".gif" };
        foreach (string ex in allowedExtensions)
        {
            if (ex == fileExtension)
            {
                fileOK = true;
                break;
            }
        }



        if (!fileOK)
        {
            ExtAspNet.Alert.Show("请选择正确的图片格式");
            return;
        }
        string fileName = filePhoto.ShortFileName;
        fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
        fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;
        if (filePhoto.HasFile)
        {
            filePhoto.SaveAs(Server.MapPath(ConfigHelper.SystemUrl + "upload/" + fileName));
            filePath = "upload/" + fileName;
        }
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {

        KeMei.BLL.FlashScreen bll = new KeMei.BLL.FlashScreen();

        KeMei.Model.FlashScreen model = new KeMei.Model.FlashScreen();

        try
        {
            model.TName = txtName.Text;
            model.IsShow = cbIsShow.Checked;
            string filePath = string.Empty;
            this.SaveFile(out filePath);
            model.PicUrl = filePath;

            if (Request["ID"] == null)
            {
                model.Recorddate = DateTime.Now;
                model.Taxis = 0;

                bll.Add(model);

            }
            else
            {
                model.ID = int.Parse(Request["ID"]);

                model.UpdateDate = DateTime.Now;

                bll.Update(model);
            }

            ExtAspNet.Alert.Show("保存成功！", String.Empty, ExtAspNet.Alert.DefaultIcon, ExtAspNet.ActiveWindow.GetHidePostBackReference());
        }
        catch (Exception ex)
        {

            ExtAspNet.Alert.Show("保存失败：" + ex.Message);
        }

    }

    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        KeMei.BLL.FlashScreen bll = new KeMei.BLL.FlashScreen();
        try
        {
            bll.SetPicUrl(int.Parse(Request["ID"].ToString()), "");
            FileHelper.DeleteFile(Server.MapPath(ConfigHelper.SystemUrl + hfPicUrl.Text));

            
            hyplnkLook.Hidden = true;
            lnkDelete.Hidden = true;
            hfPicUrl.Text = "";

            ExtAspNet.Alert.Show("删除成功");
        }
        catch (Exception ex)
        {
            ExtAspNet.Alert.Show("删除失败：" + ex.Message);
        }

    }
}